package com.lzd.framework.springsecurity.security;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import com.lzd.framework.springsecurity.model.SysUser;


/**
 * token增强
 * @author LZD
 */
@Component
public class AdminTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>(8);
        Oauth2User oauth2User = (Oauth2User) authentication.getUserAuthentication().getPrincipal();

        additionalInfo.put("userId", oauth2User.getUserId());
        additionalInfo.put("authorities", oauth2User.getAuthorities());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}
